﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace View.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpPost]
        public ActionResult Contact(Models.ContactModel data)
        {

            System.Text.StringBuilder sbMessage = new System.Text.StringBuilder();
            sbMessage.Append(string.Format("Mensagem de Contato: {0}- Nome: {1}{0}- E-mail: {2}{0}- Assunto: {3}{0}- Message: {0}{0} {4} ", System.Environment.NewLine, data.Name, data.Email, data.Assunto, data.Texto));

            string smtpServer = ConfigurationManager.AppSettings["SmptServer"].ToString();
            string emailContact = ConfigurationManager.AppSettings["EmailContact"].ToString();
            string emailPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
            int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmptPort"].ToString());

            SmtpClient client = new SmtpClient(smtpServer, smtpPort);
            client.Credentials = new NetworkCredential(emailContact, emailPassword);
            client.EnableSsl = true;
            //client.Send(Properties.Settings.Default.EmailLogFrom.Trim(), Properties.Settings.Default.EmailLogTo.Trim(), exception.Message, exception.StackTrace);
            client.Send(emailContact, emailContact, "[Publisinos] Contato: "+ data.Assunto, sbMessage.ToString());            

            ViewData["SentContact"] = true;

            return View();
        }
    }
}
